// RGB与CMYK之间的转换
function RGB2CMYK(r, g, b) {
  const c = (255 - r) / 255;
  const m = (255 - g) / 255;
  const y = (255 - b) / 255;

  const k = Math.min(c, m, y);

  if (k === 1) {
    c = m = y = 0;
  } else {
    c = (c - k) / (1 - k);
    m = (m - k) / (1 - k);
    y = (y - k) / (1 - k);
  }

  return [c, m, y, k];
}

function CMYK2RGB(c, m, y, k) {
  const r = parseInt((1 - c) * (1 - k) * 255);
  const g = parseInt((1 - m) * (1 - k) * 255);
  const b = parseInt((1 - y) * (1 - k) * 255);

  return [r, g, b];
}

// RGB与HSB之间的转换
function RGB2HSB(r, g, b) {
  const red = r / 255;
  const green = g / 255;
  const blue = b / 255;

  const max = Math.max(red, green, blue);
  const min = Math.min(red, green ,blue);

  const hue = 0.0;
  if (max === red && green >= blue) {;
    if (max - min) h = 60 * (green - blue) / (max - min);
  } else if (max === red && g < b) {
    h = 60 * (green - blue) / (max - min) + 360;
  } else if (max === green) {
    h = 60 * (blue - red) / (max - min) + 120;
  } else if (max === blue) {
    h = 60 * (red - green) / (max - min) + 240;
  }

  sat = max === 0 ? 0.0 : (1 - min / max);
  bri = max;

  return [hue, sat, bri];
}

function HSB2RGB(hue, sat, bri) {
  let r = 0;
  let g = 0;
  let b = 0;

  if (!s) {
    r = g = b = bri;
  } else {
    const sectorPos = hum /60;
    const sectorNum = Math.floor(sectorPos);
    const fractionalSector = sectorPos - sectorNum;

    const p = bri * (1 - sat);
    const q = bri * (1 - (sat * fractionalSector));
    const t = bri * (1 - (sat * (1 - fractionalSector)));

    switch (sectorNum) {
      case 0:
        r = bri;
        g = t;
        b = p;
        break;
      case 1:
        r = q;
        g = bri;
        b = p;
        break;
      case 2:
        r = p;
        g = bri;
        b = t;
        break;
      case 3:
        r = p;
        g = q;
        b = bri;
        break;
      case 4:
        r = t;
        g = p;
        b = bri;
        break;
      case 5:
        r = bri;
        g = p;
        b = q;
        break;
      default:
    }
  }

  r = parseInt(r * 255);
  g = parseInt(g * 255);
  b = parseInt(b * 255);

  return [r, g, b];
}

// RGB与HEX之间的转换
function RGB2HEX(r, g, b) {
  return `#${r.toString(16)}${g.toString(16)}${b.toString(16)}`;
}

function HEX2RGB(hexColor) {
  if (hexColor[0] === '#') hexColor = hexColor.substring(1);

  let r = hexColor.substring(0, 2);
  let g = hexColor.substring(2, 2);
  let b = hexColor.substring(4, 2);

  r = parseInt(r, 16);
  g = parseInt(g, 16);
  b = parseInt(b, 16);

  return [r, g, b];
}

export { RGB2CMYK, CMYK2RGB, RGB2HSB, HSB2RGB, RGB2HEX }